<?php
namespace app\admin\controller;
use \think\Controller;
use \think\Session;
use \think\Db;
class Formcity extends Common
{
	public function addcity(){
		return $this->fetch();
	}

  public function editcity(){
    $city = Db::name('city')->where('city_id',input('cityid'))->find();
    $this->assign('city',$city);
    return $this->fetch();
  }


	public function add(){
	   $info = '';
     if(request()->isPost()){  	
        if(input('param.cityname')==''){
          $this->error("请填写完整信息",'formcity/addcity');
        }
        $result = Db::name('city')->where('city_name',input('param.cityname'))->find();
        if($result){
           $this->error("城市名重复",'formcity/addcity');
        }
        $data=[
          'city_name'=>input('cityname'),
          'city_status'=>0
       	];
        $res=Db::name('city')->insert($data);
        if ($res) {
        	$this->success("添加城市成功",'city/index');
        }
        else {
        	$this->error("添加城市失败",'city/index');
        }
    }
	}

  public function edit(){
    $info = '';
     if(request()->isPost()){   
        if(input('param.cityname')==''){
          $this->error("请填写完整信息");
        }
        $result = Db::name('city')->where('city_name',input('param.cityname'))->find();
        if($result&&$result['city_id']!=input('cityid')){
           $this->error("城市名重复");
        }
        $data=[
          'city_name'=>input('cityname'),
        ];
        $res=Db::name('city')->where('city_id',input('cityid'))->update($data);
        if ($res!==false){
          $this->success("修改城市名成功",'city/index');
        }
        else {
          $this->error("修改城市名失败",'city/index');
        }
      }
  }

  public function delete(){
    $res = Db::name('city')->where('city_id',input('cityid'))->update(['city_status'=>input('statu')]);
    if($res===false){
        $this->error("处理失败");
    }
    $this->redirect('city/index');
  }
}